package com.xebest.web.controller.venmanager;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xebest.api.dto.PayUmApplyDTO;
import com.xebest.api.util.OpertorLogUtil;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.common.enums.logtype.ManLogTypeEnum;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.web.form.PayUmApplyForm;
import com.xebest.web.service.PayUmApplyService;
import com.xebest.web.view.ErpUserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 联动炼金相关
 *
 * @author 任建波
 */


@Controller
@RequestMapping("/payUmApply")
public class PayUmApplyController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    PayUmApplyService payUmApplyService;

    /**
     * 联动链金企业注册
     * @param form
     * @param bindingResult
     * @return
     */
    @RequestMapping("/addPayUmApplyEnterprise")
    @ResponseBody
    public BaseResponse addPayUmApplyEnterprise(
            @Validated(value= {PayUmApplyForm.Default.class, PayUmApplyForm.Enterprise.class}) @RequestBody PayUmApplyForm form,
            BindingResult bindingResult){

        logger.info("联动链金企业/个体注册，{}", JSON.toJSONString(form));
        if(bindingResult.hasErrors()){
            return sendAjaxError(bindingResult.getFieldError().getDefaultMessage());
        }

        //登陆人信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());

        PayUmApplyForm payUmApplyForm = payUmApplyService.getpayUmApplyByUserCode(userInfo.getUserCode());
        if (payUmApplyForm != null) {
            return sendAjaxError("你已经注册过银行卡了");
        }

        try {
            BaseResponse sendAjax = payUmApplyService.addPayUmApplyEnterprise(form, userInfo);

            // 添加操作日志信息
            StringBuffer content = new StringBuffer();
            content.append("联动链金企业注册").append(JSONObject.toJSONString(form));
            OpertorLogUtil.opertorLog.sendOperatorLogByParam(getRequestIP(),null, ManLogTypeEnum.manPayCfca.getValue(), userInfo.getLoginName(), content.toString(), null);

            return sendAjax;
        }catch (Exception e){
            logger.error("联动链金企业注册异常，{}",e);
            return sendAjaxError("操作异常");
        }
    }

    /**
     * 注册个人联动链金，获取短信验证码
     * @param form
     * @param bindingResult
     * @return
     */
    @RequestMapping("/getSmsCode")
    @ResponseBody
    public BaseResponse getSmsCode(@Validated(value={PayUmApplyForm.Default.class}) @RequestBody PayUmApplyForm form, BindingResult bindingResult){
        logger.info("联动链金个人注册，{}", JSON.toJSONString(form));
        //验证参数
        if(bindingResult.hasErrors()){
            return sendAjaxError(bindingResult.getFieldError().getDefaultMessage());
        }

        //登陆人信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());

        //判断是否已经注册
        PayUmApplyForm payUmApplyForm = payUmApplyService.getpayUmApplyByUserCode(userInfo.getUserCode());
        if (payUmApplyForm != null) {
            return sendAjaxError("你已经注册过银行卡了");
        }

        try {
            BaseResponse sendAjax = payUmApplyService.addPayUmApplyPerson(form, userInfo);

            // 添加操作日志信息
            StringBuffer content = new StringBuffer();
            content.append("联动链金企业注册").append(JSONObject.toJSONString(form));
            OpertorLogUtil.opertorLog.sendOperatorLogByParam(getRequestIP(),null, ManLogTypeEnum.manPayCfca.getValue(), userInfo.getLoginName(), content.toString(), null);

            return sendAjax;
        }catch (Exception e){
            logger.error("联动链金企业注册异常，{}",e);
            return sendAjaxError("操作异常");
        }
    }

    /*
    绑定银行卡-确认
     */
    @RequestMapping(value="/bindcardConfirmPerson")
    @ResponseBody
    public BaseResponse bindcardConfirmPerson(@Validated(value= {PayUmApplyForm.Default.class, PayUmApplyForm.BindcardConfirmPerson.class}) @RequestBody PayUmApplyForm form, BindingResult bindingResult) {
        //登陆人信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());

        BaseResponse sendAjax = payUmApplyService.bindcardConfirm(form, userInfo);

        return sendAjax;
    }

    /*
    获取联动链金注册信息
     */
    @RequestMapping(value="/getMypayUmApply")
    @ResponseBody
    public BaseResponse getMypayUmApply() {
        //登陆人信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());

        PayUmApplyForm payUmApplyForm = payUmApplyService.getpayUmApplyByUserCode(userInfo.getUserCode());

        Map<String,Object> map = new HashMap<>();
        map.put("payUmApply", payUmApplyForm);
        return sendAjaxOK(map);
    }

    /**
     * 通过usercode获取商户信息
     * @return
     */
    @RequestMapping(value="/getUmApplyByUserCode")
    @ResponseBody
    public PayUmApplyDTO getUmApplyByUserCode(@RequestBody PayUmApplyDTO payUmApplyDTO) {
        PayUmApplyForm payUmApplyForm = payUmApplyService.getpayUmApplyByUserCode(payUmApplyDTO.getUserCode());
        BeanUtils.copyProperties(payUmApplyForm, payUmApplyDTO);
        return payUmApplyDTO;
    }
}
